<?php

class FieldTableControl extends Control {

	private $width = '50px';
	public function WithWidth($value){ $this->width = $value; return $this; }

	private $label_width = 50;
	public function WithLabelWidth($value){ $this->label_width = $value; return $this; }

	private $labels_on_top = false;
	public function WithLabelsOnTop($value){ $this->labels_on_top = $value; return $this; }

	private $title = null;
	public function WithTitle($value){ $this->title = $value; return $this; }

	private $labels = array();
	private $contents = array();
	private $stars = array();
	private $validators = array();

	public function Add($that){
		if ($that instanceof UIField || $that instanceof MetaField){
			$this->labels[] = $that->GetLabel();
			$this->contents[] = '';
		}
		elseif ($that instanceof ValueControl){
			$this->labels[] = $that->label;
			$this->contents[] = '' . $that;
		}
		else {
			$this->labels[] = $that;
			$this->contents[] = '';
		}
		$this->stars[] = false;
		$this->validators[] = null;
		return $this;
	}

	public function AddOKCancel(){
		$this->Add('');
		$this->Write(ButtonControl::Make()->WithIsSubmit(true)->WithValue(Lemma::OK()).'&nbsp;'.ButtonControl::Make()->WithValue(Lemma::Cancel())->WithOnClick("history.back();"));
	}

	public function AddSubmitReset(){
		$this->Add('');
		$this->Write(ButtonControl::Make()->WithValue(Lemma::OK())->WithIsSubmit(true).'&nbsp;'.ButtonControl::Make()->WithValue(Lemma::Cancel())->WithIsReset(true));
	}


	public function Write($that){
		$this->contents[count($this->contents) - 1] .= $that;
		return $this;
	}

	public function WithStar(){
		$this->stars[count($this->stars) - 1] = true;
		return $this;
	}
	public function WithValidator($validator){
		$this->validators[count($this->stars) - 1] = $validator;
		return $this;
	}

	public function Render(){

		if ($this->labels_on_top){
			echo '<table class="fieldtable" cellspacing="0" cellpadding="0" border="0" style="width:'.(empty($this->width)?'auto':$this->width).';">';

			if (!is_null($this->title)) echo '<tr><td><h2>'.$this->title.'</h2></td></tr>';

			for ($i=0; $i<count($this->labels); $i++){
				if ($this->labels[$i]=='-'){
					echo '<tr><td style="border-bottom:1px solid #cccccc;line-height:1px;">'.new Spacer(1,10).'</td></tr>';
					echo '<tr><td style="line-height:1px;">'.new Spacer(1,5).'</td></tr>';
				}
				else{
					echo '<tr><td style="text-align:left;padding:15px 1px 1px 1px;">'.$this->labels[$i].($this->stars[$i]?' *':'').'</td></tr>';
					echo '<tr><td style="text-align:left;">'.$this->contents[$i].'</td></tr>';
					if ($this->validators[$i] != null) if (!$this->validators[$i]->HasPassed())
						echo '<tr><td style="text-align:left;padding:1px 0 0 0">'.new MessageControl($this->validators[$i]).'</td></tr>';
				}
			}
			echo '</table>';
		}
		else {
			echo '<table class="fieldtable" cellspacing="5" cellpadding="0" border="0" style="width:'.(empty($this->width)?'auto':$this->width).';">';
			if (!is_null($this->title)) echo '<tr><td>&nbsp;</td><td><h2>'.$this->title.'</h2></td></tr>';

			for ($i=0; $i<count($this->labels); $i++){
				if ($this->labels[$i]=='-'){
					echo '<tr><td style="line-height:1px;">'.new Spacer().'</td><td style="border-bottom:1px solid #cccccc;line-height:1px;">'.new Spacer(1,10).'</td></tr>';
					echo '<tr><td style="line-height:1px;">'.new Spacer().'</td><td style="line-height:1px;">'.new Spacer(1,5).'</td></tr>';
				}
				else{
				echo '<tr>';
				echo '<td class="nowrap vtop hright" style="padding-top:3px;width:"'.$this->label_width.'px;">'.($this->stars[$i]?'* ':'').$this->labels[$i].'</td>';
				echo '<td class="expand hleft">'.$this->contents[$i];
				if ($this->validators[$i] != null)
					if (!$this->validators[$i]->HasPassed())
						echo '<div style="padding:2px 0 0 0">'.new MessageControl($this->validators[$i]).'</div>';
				echo '</td>';
				echo '</tr>';
					}
			}
			echo '</table>';
		}
	}


}


?>